package com.hotelsystem.service.impl;

import com.hotelsystem.dao.RoomMapper;
import com.hotelsystem.dao.RoomTypeMapper;
import com.hotelsystem.pojo.Room;
import com.hotelsystem.pojo.RoomType;
import com.hotelsystem.pojo.vo.RoomQueryVo;
import com.hotelsystem.service.RoomService;
import com.hotelsystem.util.Tool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;


@Service
@Transactional
public class RoomServiceImpl implements RoomService {

    @Resource(name = "roomMapper")
    private RoomMapper roomMapper;
    @Resource
    private RoomTypeMapper roomTypeMapper;
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Room> selectAll() {
        return roomMapper.selectAll();
    }
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Room> getPageforRoomLists(RoomQueryVo roomQueryVo) throws Exception {
        //从获取数据
        return roomMapper.getPageforRoomLists(roomQueryVo);
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public Room getRoomInfoWithId(String roomId) throws Exception {
        return roomMapper.getRoomInfoWithId(roomId);
    }


    /**
     * 删除RoomInfo时修改RoomType
     * @param room
     */
    public void updateTypeByUpdateRoomInfoByReduce(Room room){

        //修改roomtypeInfo
       RoomType  roomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());
        /**
         * 当前类型房屋下的数量
         */
        roomType.setRoomNum(roomType.getRoomNum()-1);
        //当前房屋可用的房间数量
        Integer  Ableresult2 = roomType.getUsable();

        //建总数量
        if (roomType.getRoomNum()>0){
            roomType.setRoomNum(roomType.getRoomNum()-1);
        }
        //减可用数量
        if (room.getStatus().getStatusId() == 3 || room.getStatus().getStatusId() ==4 ){
            roomType.setUsable(Ableresult2-1);
        }

        //在这里更改roomType的状态
        //0：房间已满、1：可预订可入住、2：无房间
        if (Ableresult2>0){
            roomType.setStatus(1);
        }else if (Ableresult2 == 0 && (roomType.getReserved()>0 || roomType.getCheckIn()>0)){
            roomType.setStatus(0);
        }else if (Ableresult2 == 0 && roomType.getRoomNum() ==0){
            roomType.setStatus(2);
        }
        updateRoomTypeStatusByroomType(roomType);
    }
    /**
     *
     * @param roomType
     */
    public void updateRoomTypeStatusByroomType(RoomType roomType){
        if (roomType.getStatus() > 0){
            roomType.setStatus(1);
        }else if (roomType.getRoomNum() == (roomType.getCheckIn()+roomType.getReserved()) && (roomType.getCheckIn()+roomType.getReserved())>0 ){
            roomType.setStatus(0);
        }else if(roomType.getRoomNum() == 0){
            roomType.setStatus(2);
        }
        roomTypeMapper.updateRoomTypeByRoomType(roomType);
    }

    /**
     * 添加RoomInfo时修改RoomType
     * @param room
     */
    public void updateTypeByUpdateRoomInfoByAdd(Room room){

        //获得状态
        Integer  oldStatusId = room.getStatus().getStatusId();
        //获得原来的roomtypeInfo
        RoomType  roomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());
        roomType.setRoomNum(roomType.getRoomNum()+1);


        if ( oldStatusId == 3 || oldStatusId == 4 )
        {
            roomType.setUsable(roomType.getUsable()+1);
        }
      if (oldStatusId == 1)
          roomType.setCheckIn(roomType.getCheckIn()+1);
      else if (oldStatusId == 2){
          roomType.setReserved(roomType.getReserved()+1);
      }
      Integer  Ableresult2 = roomType.getUsable();
        //在这里更改roomType的状态
        //0：房间已满、1：可预订可入住、2：无房间
        if (Ableresult2>0){
            roomType.setStatus(1);
        }else if (Ableresult2 == 0 && (roomType.getReserved()>0 || roomType.getCheckIn()>0)){
            roomType.setStatus(0);
        }else if (Ableresult2 == 0 && roomType.getRoomNum() ==0){
            roomType.setStatus(2);
        }

        roomTypeMapper.updateRoomTypeByRoomType(roomType);
    }
    /**
     * 先修改roomType数据库中的值 然后在删除
     * @param roomId
     * @return
     * @throws Exception
     */
    @Override
    public Integer deleteRoomInfoWithId(String roomId) throws Exception {
        //修改roomtype中的数据
        Room room = getRoomInfoWithId(roomId);
        updateTypeByUpdateRoomInfoByReduce(room);
        return roomMapper.deleteRoomInfoWithId(roomId);
    }

    @Override
    public void addRoomInfoWithRoom(Room room,String imgBase64) throws Exception {

        if (imgBase64!=null){
            File upload = createRoomFolder();

            System.out.println("upload url:"+upload.getAbsolutePath());
            String  absolutePath = upload.getAbsolutePath();
            String fileName = Tool.createUUID() + ".png";

            String fullPath =  absolutePath+"/"+fileName;
            System.out.println("fullPath"+fullPath);

            try {
                Tool.writeImgBase64ToFile(imgBase64, fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            room.setRoomImage(fileName);
        }
        if (imgBase64==null){
            room.setRoomImage("3.jpg");
        }

        updateTypeByUpdateRoomInfoByAdd(room);
        roomMapper.addRoomInfoByRoom(room);

    }


    @Override
    public void updateRoomInfoByRoom(Room room,String imgBase64,Integer oldTypeId,Integer oldStatusId) {
        //获得原来的图片
        Room roomForImg = null;
        try {
            roomForImg = getRoomInfoWithId(room.getRoomId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果有传过来的图片，说明就要修改
        if (imgBase64!=null && imgBase64 !=""){
            //通过Id查询出原来的Img
            try {

                String fileImg = roomForImg.getRoomImage();
                //这里给完整路径，不然别人不知道哪里删除
                File upload = createRoomFolder();
                String fullPath = upload.getAbsolutePath()+"/"+fileImg;
                //先删除原来的图片
                if (fileImg!=null)
                    deleteFile(fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //添加新图片
            File upload = createRoomFolder();

            System.out.println("upload url:"+upload.getAbsolutePath());
            String fileName = Tool.createUUID() + ".png";
            String fullPath = upload.getAbsolutePath()+"/"+fileName;
            try {
                Tool.writeImgBase64ToFile(imgBase64, fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //再添加现有的个图片
            room.setRoomImage(fileName);
        }else{
            //否则就不修改
            room.setRoomImage(roomForImg.getRoomImage());
        }
        /**
         * 思路：
         * 一 。先判断当前的房间类型是否修改 改变
         *      1.如果类型发生改变
         *          比较房间状态是否发生改变
         *              改变 和 未改变
         *       2.如果类型没有发生改变
         *  二。 房间类型没有发生改变
         *      1.如果类型发生改变
         *          *          比较房间状态是否发生改变
         *          *              改变 和 未改变
         *          *       2.如果类型没有发生改变
         *
         */
        //判断现在的和之前的是否一致
        //当修改前的类型 和 修改后的类型不相等
      /*  if (room.getRoomType().getRoomTypeId() != oldTypeId){
             // 房间类型 和 房间状态 改变

            if (room.getStatus().getStatusId() != oldStatusId){
                //修改旧的RoomType信息
                //当前类型发生改变 老的就要减一
                RoomType oldRoomType = roomTypeMapper.getRoomTypeInfoById(oldTypeId);
                System.out.println("在这里看看是否减一："+ oldRoomType);

                System.out.println("----"+oldRoomType.getRoomNum());
                oldRoomType.setRoomNum(oldRoomType.getRoomNum()-1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
                Integer Ableresult1 = roomTypeMapper.getAbleCheckInAndReserveSum(oldTypeId);
                System.out.println("老的类型的还剩下的可用类型："+Ableresult1);
                //在这里判断是否是可入住 还是可预定
                if (oldStatusId == 1){
                    oldRoomType.setCheckIn(oldRoomType.getCheckIn() - 1);
                }else if (oldStatusId == 2){
                    oldRoomType.setReserved(oldRoomType.getReserved()-1);
                }else if ( oldStatusId == 3 || oldStatusId == 4 )
                {
                    Ableresult1 = Ableresult1 -1;
                }
                oldRoomType.setUsable(Ableresult1);
                //调用通用修改方法 修改类型的状态方法
                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult1>0){
                    oldRoomType.setStatus(1);
                }else if (Ableresult1 == 0 && (oldRoomType.getReserved()>0 || oldRoomType.getCheckIn()>0)){
                    oldRoomType.setStatus(0);
                }else if (Ableresult1 == 0 && oldRoomType.getRoomNum() ==0){
                    oldRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(oldRoomType);


                //修改新的roomType信息
                RoomType newRoomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());

                newRoomType.setRoomNum(newRoomType.getRoomNum()+1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
                Integer Ableresult2 = roomTypeMapper.getAbleCheckInAndReserveSum(room.getRoomType().getRoomTypeId());
                Integer newStatusId = room.getStatus().getStatusId();
                //在这里判断是否是可入住 还是可预定
                if (newStatusId == 1){
                    newRoomType.setCheckIn(newRoomType.getCheckIn() + 1);
                }else if (newStatusId == 2){
                    newRoomType.setReserved(newRoomType.getReserved()+1);
                }else if ( newStatusId == 3 || newStatusId == 4 )
                {
                    //这里切换为
                    Ableresult2 = Ableresult2 +1;
                }
                newRoomType.setUsable(Ableresult2);

                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult2>0){
                    newRoomType.setStatus(1);
                }else if (Ableresult2 == 0 && (newRoomType.getReserved()>0 || newRoomType.getCheckIn()>0)){
                    newRoomType.setStatus(0);
                }else if (Ableresult2 == 0 && newRoomType.getRoomNum() ==0){
                    newRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(newRoomType);
                //房间类型发生改变，房间状态不变
            }else if (room.getStatus().getStatusId() == oldStatusId){
                //修改旧的RoomType信息
                //当前类型发生改变 老的就要减一
                RoomType oldRoomType = roomTypeMapper.getRoomTypeInfoById(oldTypeId);
                System.out.println("在这里看看是否减一："+ oldRoomType);

                System.out.println("----"+oldRoomType.getRoomNum());
                oldRoomType.setRoomNum(oldRoomType.getRoomNum()-1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
                Integer Ableresult1 = roomTypeMapper.getAbleCheckInAndReserveSum(oldTypeId);
                System.out.println("老的类型的还剩下的可用类型："+Ableresult1);
                //在这里判断是否是可入住 还是可预定
                if (oldStatusId == 1){
                    oldRoomType.setCheckIn(oldRoomType.getCheckIn() - 1);
                }else if (oldStatusId == 2){
                    oldRoomType.setReserved(oldRoomType.getReserved()-1);
                }else if ( oldStatusId == 3 || oldStatusId == 4 )
                {
                    Ableresult1 = Ableresult1 -1;
                }
                oldRoomType.setUsable(Ableresult1);
                //调用通用修改方法 修改类型的状态方法
                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult1>0){
                    oldRoomType.setStatus(1);
                }else if (Ableresult1 == 0 && (oldRoomType.getReserved()>0 || oldRoomType.getCheckIn()>0)){
                    oldRoomType.setStatus(0);
                }else if (Ableresult1 == 0 && oldRoomType.getRoomNum() ==0){
                    oldRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(oldRoomType);


                //修改新的roomType信息
                RoomType newRoomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());

                newRoomType.setRoomNum(newRoomType.getRoomNum()+1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
                Integer Ableresult2 = roomTypeMapper.getAbleCheckInAndReserveSum(room.getRoomType().getRoomTypeId());
                Integer newStatusId = room.getStatus().getStatusId();
                //在这里判断是否是可入住 还是可预定
                if (newStatusId == 1){
                    newRoomType.setCheckIn(newRoomType.getCheckIn() + 1);
                }else if (newStatusId == 2){
                    newRoomType.setReserved(newRoomType.getReserved()+1);
                }else if ( newStatusId == 3 || newStatusId == 4 )
                {
                    //这里切换为
                    Ableresult2 = Ableresult2 +1;
                }
                newRoomType.setUsable(Ableresult2);

                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult2>0){
                    newRoomType.setStatus(1);
                }else if (Ableresult2 == 0 && (newRoomType.getReserved()>0 || newRoomType.getCheckIn()>0)){
                    newRoomType.setStatus(0);
                }else if (Ableresult2 == 0 && newRoomType.getRoomNum() ==0){
                    newRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(newRoomType);
            }

            //修改前和修改后的相等
        }else{
            //房间类型不改变，房间状态改变
            if (room.getStatus().getStatusId() != oldStatusId) {

                //修改旧的RoomType信息
                //当前类型发生改变 老的就要减一
                RoomType oldRoomType = roomTypeMapper.getRoomTypeInfoById(oldTypeId);
                System.out.println("在这里看看是否减一："+ oldRoomType);
                oldRoomType.setRoomNum(oldRoomType.getRoomNum()-1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
                //Integer Ableresult1 = roomTypeMapper.getAbleCheckInAndReserveSum(oldTypeId);
                Integer Ableresult1 = oldRoomType.getUsable();
                System.out.println("老的类型的还剩下的可用类型："+Ableresult1);
                //在这里判断是否是可入住 还是可预定
                if (oldStatusId == 1){
                    oldRoomType.setCheckIn(oldRoomType.getCheckIn() - 1);
                }else if (oldStatusId == 2){
                    oldRoomType.setReserved(oldRoomType.getReserved()-1);
                }else if ( oldStatusId == 3 || oldStatusId == 4 )
                {
                    System.out.println(oldStatusId);
                    System.out.println("要执行减一");
                    Ableresult1 = Ableresult1 -1;
                }
                System.out.println("oldStatusId"+Ableresult1);
                oldRoomType.setUsable(Ableresult1);
                //调用通用修改方法 修改类型的状态方法
                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult1>0){
                    oldRoomType.setStatus(1);
                }else if (Ableresult1 == 0 && (oldRoomType.getReserved()>0 || oldRoomType.getCheckIn()>0)){
                    oldRoomType.setStatus(0);
                }else if (Ableresult1 == 0 && oldRoomType.getRoomNum() ==0){
                    oldRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(oldRoomType);


                //修改新的roomType信息
                RoomType newRoomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());
                System.out.println("newroomType"+newRoomType);
                newRoomType.setRoomNum(newRoomType.getRoomNum()+1);
                //在这里判断是否是可入住 还是可预定
                //获得可入住+可预订的数量
               // Integer Ableresult2 = roomTypeMapper.getAbleCheckInAndReserveSum(room.getRoomType().getRoomTypeId());
                Integer Ableresult2 = newRoomType.getUsable();
                System.out.println("可预订+可入住"+Ableresult2);
                Integer newStatusId = room.getStatus().getStatusId();
                //在这里判断是否是可入住 还是可预定
                if (newStatusId == 1){
                    newRoomType.setCheckIn(newRoomType.getCheckIn() + 1);
                }else if (newStatusId == 2){
                    newRoomType.setReserved(newRoomType.getReserved()+1);
                }else if ( newStatusId == 3 || newStatusId == 4 )
                {
                    System.out.println(newStatusId);
                    System.out.println("要执行加一");
                    //这里切换为
                    Ableresult2 = Ableresult2 +1;
                }
                System.out.println("newStatusId"+Ableresult2);
                newRoomType.setUsable(Ableresult2);

                //在这里更改roomType的状态
                //0：房间已满、1：可预订可入住、2：无房间
                if (Ableresult2>0){
                    newRoomType.setStatus(1);
                }else if (Ableresult2 == 0 && (newRoomType.getReserved()>0 || newRoomType.getCheckIn()>0)){
                    newRoomType.setStatus(0);
                }else if (Ableresult2 == 0 && newRoomType.getRoomNum() ==0){
                    newRoomType.setStatus(2);
                }
                roomTypeMapper.updateRoomTypeByRoomType(newRoomType);
                //房间类型不改变，房间状态不改变
            }else if(room.getStatus().getStatusId() == oldStatusId){
                //不做操作
            }
        }
       */
       if (room.getRoomType().getRoomTypeId() != oldTypeId || room.getStatus().getStatusId() != oldStatusId){
           //修改旧的RoomType信息
           //当前类型发生改变 老的就要减一
           RoomType oldRoomType = roomTypeMapper.getRoomTypeInfoById(oldTypeId);
           System.out.println("在这里看看是否减一："+ oldRoomType);
           oldRoomType.setRoomNum(oldRoomType.getRoomNum()-1);
           //在这里判断是否是可入住 还是可预定
           //获得可入住+可预订的数量  注意：在这里会出现线程问题
           //Integer Ableresult1 = roomTypeMapper.getAbleCheckInAndReserveSum(oldTypeId);
           Integer Ableresult1 = oldRoomType.getUsable();
           System.out.println("老的类型的还剩下的可用类型："+Ableresult1);
           //在这里判断是否是可入住 还是可预定
           if (oldStatusId == 1){
               oldRoomType.setCheckIn(oldRoomType.getCheckIn() - 1);
           }else if (oldStatusId == 2){
               oldRoomType.setReserved(oldRoomType.getReserved()-1);
           }else if ( oldStatusId == 3 || oldStatusId == 4 )
           {
               System.out.println(oldStatusId);
               System.out.println("要执行减一");
               Ableresult1 = Ableresult1 -1;
           }
           System.out.println("oldStatusId"+Ableresult1);
           oldRoomType.setUsable(Ableresult1);
           //调用通用修改方法 修改类型的状态方法
           //在这里更改roomType的状态
           //0：房间已满、1：可预订可入住、2：无房间
           if (Ableresult1>0){
               oldRoomType.setStatus(1);
           }else if (Ableresult1 == 0 && (oldRoomType.getReserved()>0 || oldRoomType.getCheckIn()>0)){
               oldRoomType.setStatus(0);
           }else if (Ableresult1 == 0 && oldRoomType.getRoomNum() ==0){
               oldRoomType.setStatus(2);
           }
           roomTypeMapper.updateRoomTypeByRoomType(oldRoomType);


           //修改新的roomType信息
           RoomType newRoomType = roomTypeMapper.getRoomTypeInfoById(room.getRoomType().getRoomTypeId());
           System.out.println("newroomType"+newRoomType);
           newRoomType.setRoomNum(newRoomType.getRoomNum()+1);
           //在这里判断是否是可入住 还是可预定
           //获得可入住+可预订的数量 注意：这里可能会出现线程问题
           //Integer Ableresult2 = roomTypeMapper.getAbleCheckInAndReserveSum(room.getRoomType().getRoomTypeId());
            Integer Ableresult2 = newRoomType.getUsable();
           System.out.println("可预订+可入住"+Ableresult2);
           Integer newStatusId = room.getStatus().getStatusId();
           //在这里判断是否是可入住 还是可预定
           if (newStatusId == 1){
               newRoomType.setCheckIn(newRoomType.getCheckIn() + 1);
           }else if (newStatusId == 2){
               newRoomType.setReserved(newRoomType.getReserved()+1);
           }else if ( newStatusId == 3 || newStatusId == 4 )
           {
               System.out.println(newStatusId);
               System.out.println("要执行加一");
               //这里切换为
               Ableresult2 = Ableresult2 +1;
           }
           System.out.println("newStatusId"+Ableresult2);
           newRoomType.setUsable(Ableresult2);

           //在这里更改roomType的状态
           //0：房间已满、1：可预订可入住、2：无房间
           if (Ableresult2>0){
               newRoomType.setStatus(1);
           }else if (Ableresult2 == 0 && (newRoomType.getReserved()>0 || newRoomType.getCheckIn()>0)){
               newRoomType.setStatus(0);
           }else if (Ableresult2 == 0 && newRoomType.getRoomNum() ==0){
               newRoomType.setStatus(2);
           }
           roomTypeMapper.updateRoomTypeByRoomType(newRoomType);
       }
        roomMapper.updateRoomInfoByRoomId(room);
    }

    /**
     * 刪除单个文件 如果文件存在就删除，如果不存在就不删
     * @param fileName
     */
    public  void deleteFile(String fileName) {
        File f =  createRoomFolder();
        String fullPath = f.getAbsolutePath()+"/"+fileName;
        File file = new File(fullPath);
        //这里判断文件是否存在
        if (fileName!=null && file.exists() && file.isFile()){
            try {
                if (file.delete()) {
                    System.out.println(file.getName() + "file is deleted");
                } else {
                    System.out.println("file Delete failed.");
                }
            } catch (Exception e) {
                System.out.println("Exception occured for file Delete");
                e.printStackTrace();
            }
        }
    }

    private File createRoomFolder(){
        //获取根路径的方法
        File path = new File(System.getProperty("user.dir"));
        File upload = new File(path.getAbsolutePath(),"static/upload/roomImgs");
        if(!upload.exists()) upload.mkdirs();
        return upload;
    }

}